package pkg

import (
	"context"
	"github.com/qiniu/go-sdk/v7/storagev2/credentials"
	"github.com/qiniu/go-sdk/v7/storagev2/http_client"
	"github.com/qiniu/go-sdk/v7/storagev2/uploader"
	"mime/multipart"
	"video/appconfig"
)

func Upload(file *multipart.FileHeader, err error) (string, error) {
	Yun := appconfig.ConfigData.QiNiu
	accessKey := Yun.AccessKey
	secretKey := Yun.SecretKey
	mac := credentials.NewCredentials(accessKey, secretKey)
	bucket := Yun.Bucket
	key := file.Filename
	uploadManager := uploader.NewUploadManager(&uploader.UploadManagerOptions{
		Options: http_client.Options{
			Credentials: mac,
		},
	})
	of, err := file.Open()
	if err != nil {
		return "", nil
	}
	defer of.Close()
	err = uploadManager.UploadReader(context.Background(), of, &uploader.ObjectOptions{
		BucketName: bucket,
		ObjectName: &key,
		CustomVars: map[string]string{
			"name": "github logo",
		},
		FileName: key,
	}, nil)
	return Yun.Rul + key, err
}
